<?php
/**
 * 宜信阿福黑名单
 * User: GRAY
 * Date: 16/8/11
 * Time: 下午10:23
 */

namespace Home\Common\Hook;


use Common\Library\BusinessException;
use Common\Library\Constant;
use Common\Vendor\Hook\BaseHook;
use Think\Log;


class CreditEase extends BaseHook
{

    //private $_uid = 'maib_testusr';
    //private $_pwd = 'dc38d9e29bbdd37a';
    //private $_uid = 'maib';
    //private $_pwd = 'd522a6776322252b';
    //private $_pem = './zc.pem';//公钥地址
    //private $_servers = 'http://www.zhichengcredit.com/CreditPortal/api';

    public function execute($data, $param = [])
    {
        if (isset($param['uid'], $param['pwd'], $param['pem_path'], $param['api_url'])) {

            $server = $param['api_url'];
            $key = $this->rsa_encode($param['uid'],$param['pem_path']);

            //$json_data = '{"name":"王天伟","idType":"101","idNo":"610103197604041613","queryReason":"10","mobile":"13802800429"}';
            $json_data = '{"name":"'.$data['real_name'].'","idType":"101","idNo":"'.$data['id_num'].'","queryReason":"10","mobile":"'.$data['mobile'].'"}';

            $body = base64_encode($this->rc4_encode($param['pwd'], $json_data));
            $postdata  = array(
                'userid'=>$key,
                'params'=>$body
            );
            $result = $this->_send($server,$postdata);
            if($result) {
                if($result['errorcode']=='0000') {
                    $de_data = urldecode($this->rc4_decode($param['pwd'], base64_decode($result['data'])));
                    Log::write("CreditEase-RESULT:".$de_data, Log::INFO);
                    $result_data = json_decode($de_data,true);
                    if($result_data) {
                        if( count($result_data['riskItems']) > 0) {
                            BusinessException::throwException(Constant::IN_BLACK_LIST_AF);
                        }
                    }
                }
            }
            return $data;
        } else {
            BusinessException::throwException(Constant::INVALID_PARAMETER);
        }
    }

    private function _send($url,$postdata){

        $tuCurl = curl_init();
        curl_setopt($tuCurl, CURLOPT_URL, $url);
        curl_setopt($tuCurl, CURLOPT_POST, 1);
        curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $postdata);
        $header = array();
        curl_setopt($tuCurl, CURLOPT_HTTPHEADER, $header);
        $tuData = curl_exec($tuCurl);

        $tuData = json_decode($tuData,true);
        //print_r($tuData);
        if($tuData) {
            return $tuData;
        } else {
            return false;
        }
    }
	public static function rsa_encode($data,$pem_path){
		$pu_key = openssl_pkey_get_public ( file_get_contents ($pem_path) );
		openssl_public_encrypt ( $data, $encrypted, $pu_key );
		return base64_encode( $encrypted );
	}
	public static function rsa_decode($data,$pem_pat){
		$pu_key = openssl_pkey_get_public ( file_get_contents ($pem_path) );
		openssl_public_decrypt(base64_decode($data), $decrypted, $pu_key);
		return $decrypted;
	}

	public static function rc4_encode($pwd, $data) {
		$key [] = "";
		$box [] = "";
		$pwd_length = strlen ( $pwd );
		$data_length = strlen ( $data );
		
		for($i = 0; $i < 256; $i ++) {
			$key [$i] = ord ( $pwd [$i % $pwd_length] );
			$box [$i] = $i;
		}
		
		for($j = $i = 0; $i < 256; $i ++) {
			$j = ($j + $box [$i] + $key [$i]) % 256;
			$tmp = $box [$i];
			$box [$i] = $box [$j];
			$box [$j] = $tmp;
		}
		
		for($a = $j = $i = 0; $i < $data_length; $i ++) {
			$a = ($a + 1) % 256;
			$j = ($j + $box [$a]) % 256;
			
			$tmp = $box [$a];
			$box [$a] = $box [$j];
			$box [$j] = $tmp;
			
			$k = $box [(($box [$a] + $box [$j]) % 256)];
			$cipher .= chr ( ord ( $data [$i] ) ^ $k );
		}
		return $cipher;
	}
	public static function rc4_decode($pwd,$data){
		return self::rc4_encode($pwd, $data);
	}

} 


